<?php

function saveImage($url){
	$tmp=explode("://",$url);
	$tmp=explode("/",$tmp[1]);
	$filename=$tmp[count($tmp)-1];
	$image=file_get_contents($url);
	$img = 'wp-includes/tiltviewer/imgs/'.$filename;
	$realPath = 'imgs/'.$filename;
	if(!file_exists($img))
		file_put_contents($img,$image);
	return $realPath;
}

  $db = new DBManager ( 'localhost', 'root', '', 'iumus' );
  $query='SELECT * FROM movie WHERE year=2011 and cover <> "N/A" LIMIT 50;';
  $films = array();
  $doc = new DOMDocument("1.0");
  $doc->formatOutput = true;
  $root = $doc->createElement( "tiltviewergallery" );
  $doc->appendChild($root);
  $photos = $doc->createElement( "photos" );
  $root->appendChild($photos);
  $result=$db->executeQuery ($query);
  while($films=mysql_fetch_array($result)){
	$photo=$doc->createElement("photo");
	$photos->appendChild($photo);
	$imageUrlAttribute=$doc->createAttribute("imageurl");
	$linkUrlAttribute=$doc->createAttribute("linkurl");
	$pathname=saveImage($films['cover']);
	$imageUrlAttibuteValue=$doc->createTextNode($pathname);
	$linkUrlAttibuteValue=$doc->createTextNode("../../infoFilm.php?id=".$films['id']);
	$photo->appendChild($imageUrlAttribute);
	$photo->appendChild($linkUrlAttribute);
	$imageUrlAttribute->appendChild($imageUrlAttibuteValue);
	$linkUrlAttribute->appendChild($linkUrlAttibuteValue);
	$description=$doc->createElement("description");
	$genre=str_replace(',','|',$films['genre']);
	$realDescription=substr($films['plot'],0,150);
	$realDescription=$realDescription.'...';
	$tagDescription='<![CDATA[<font size="20">'.$films['rated']." - ".$films['runtime']." - ".$genre.'</font><br/><br/><font size="25">Rank:</font><b>'.$films['rating'].'</b><font size="25">/10</font><br/><br/>'.$realDescription.'<br><b>Director:</b> '.$films['director'].'<br><b>Writers:</b>'.$films['writer'].'<br><b>Stars:</b>'.$films['actors'];
	$tagDescription=$tagDescription.']]>';
	$descriptionValue=$doc->createTextNode($tagDescription);
	$description->appendChild($descriptionValue);
	$photo->appendChild($description);
}
 $doc->save("wp-includes/tiltviewer/gallery.xml");
 $tempFile= file_get_contents("wp-includes/tiltviewer/gallery.xml");
 $tempFile=str_replace('&lt;','<',$tempFile);
 $tempFile=str_replace('&gt;','>',$tempFile);
 file_put_contents("wp-includes/tiltviewer/gallery.xml",$tempFile);
 
 $_SESSION['tiltviewer'] = true;
  
?>